<?php

namespace App\Providers;

use Dingo\Api\Facade\API;
use App\Traits\ApiResponse;
use App\Services\NodeService;
use App\Services\WechatService;
use App\Services\MessageService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\ValidationException;
use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;

class AppServiceProvider extends ServiceProvider
{
    use ApiResponse;

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //频率错误
        API::error(function (ThrottleRequestsException $exception) {
            $headers  = $exception->getHeaders();
            return $this->failed('请求太过频繁，请'.custom_second_trans($headers['Retry-After']).'后再试！');
        });

        //路由错误
        API::error(function (NotFoundHttpException $exception) {
            return $this->failed('路由错误');
        });

        //请求方式错误
        API::error(function (MethodNotAllowedHttpException $exception) {
            return $this->failed('请求方式错误');
        });

        //验证器报错
        API::error(function (ValidationException $exception) {
            foreach ($exception->errors() as $key => $value){
                return $this->failed($value[0]);
            }
        });

        //Exception报错
        API::error(function (\Exception $exception) {
            return $this->failed($exception->getMessage());
        });

        //开发环境下才使用telescope
        if ($this->app->isLocal()) {
            $this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
        }

        //添加Message门面
        $this->app->singleton('Message',MessageService::class);

        $this->app->singleton('Node',NodeService::class);
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
        //记录日常sql
        DB::listen(function ($sql) {
            foreach ($sql->bindings as $i => $binding) {
                if ($binding instanceof \DateTime) {
                    $sql->bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
                } else {
                    if (is_string($binding)) {
                        $sql->bindings[$i] = "'$binding'";
                    }
                }
            }
            $query = str_replace(array('%', '?'), array('%%', '%s'), $sql->sql);
            $query = vsprintf($query, $sql->bindings);
            Log::channel('sql')->info($query);
        });

        //引入额外的绑定
        $this->bindServices();
    }

    public function bindServices()
    {
        //绑定wechat service
        $this->app->singleton(WechatService::class,function($app){
            return new WechatService();
        });
    }
}
